package com.cloudbroker.bcs.platform.dav.middle.context;

import com.cloudbroker.bcs.common.context.BizDataContext;
import com.cloudbroker.bcs.common.context.DataContext;
import com.cloudbroker.bcs.common.exception.HSBCSErrInfoException;
import com.cloudbroker.bcs.common.util.ValueUtil;
import com.cloudbroker.bcs.platform.dav.bizapi.constants.HSBCPSErrorCodes;
import com.cloudbroker.bcs.platform.dav.common.entity.ClientProfile;
import com.cloudbroker.bcs.platform.dav.middle.entity.Broker;
import com.cloudbroker.bcs.platform.dav.middle.entity.Customer;

public class HSBCPSBizDataContext {
    
    public static final String KEY_NAMESPACE = HSBCPSBizDataContext.class.getName();
    
    public static final String KEY_CLIENT_PROFILE = KEY_NAMESPACE + ".clientProfile";
    
    public static final String KEY_STAFF = KEY_NAMESPACE + ".staff";
    
    public static final String KEY_OPERATOR_ID = KEY_NAMESPACE + ".operatorId";
    
    public static final String KEY_CUSTOMER = KEY_NAMESPACE + ".customer";
    
    public static final String KEY_BROKER = KEY_NAMESPACE + ".broker";
    
    public static final String KEY_BROKER_ID = KEY_NAMESPACE + ".brokerId";
    
    public static final String KEY_CLIENT_ID = KEY_NAMESPACE + ".clientId";
    
    public static final String KEY_TENANT_ID = KEY_NAMESPACE + ".tenantId";
    
    public static Long getUserCenterId() {
        return BizDataContext.getUserCenterId();
    }
    
    public static void setUserCenterId(Long userCenterId) {
        BizDataContext.setUserCenterId(userCenterId);
    }
    
    public static String getAccess_token() {
        return BizDataContext.getAccess_token();
    }
    
    public static void setAccess_token(String access_token) {
        BizDataContext.setAccess_token(access_token);
    }
    
    public static String removeAccess_token() {
        return BizDataContext.removeAccess_token();
    }
    
    public static String getBizServiceName() {
        return BizDataContext.getBizServiceName();
    }
    
    public static void setBizServiceName(String bizServiceName) {
        BizDataContext.setBizServiceName(bizServiceName);
    }
    
    public static String removeBizServiceName() {
        return BizDataContext.removeBizServiceName();
    }
    
    public static ClientProfile removeClientProfile() {
        return (ClientProfile) remove(KEY_CLIENT_PROFILE);
    }
    
    public static Integer getOperatorId() {
        return (Integer) get(KEY_OPERATOR_ID);
    }
    
    public static void setOperatorId(Integer operatorId) {
        set(KEY_OPERATOR_ID, operatorId);
    }
    
    public static Customer getCustomer() {
        Customer customer = getCustomerNoException();
        if (customer == null) {
            throw new HSBCSErrInfoException(HSBCPSErrorCodes.NO_PERMISSION);
        }
        return customer;
    }
    
    public static Customer getCustomerNoException() {
        return (Customer) get(KEY_CUSTOMER);
    }
    
    public static Broker getBrokerNoException() {
        return (Broker) get(KEY_BROKER);
    }
    
    public static void setCustomer(Customer customer) {
        set(KEY_CUSTOMER, customer);
    }
    
    public static Broker getBroker() {
        Broker broker = getBrokerNoException();
        if (broker == null) {
            throw new HSBCSErrInfoException(HSBCPSErrorCodes.NO_PERMISSION);
        }
        return broker;
    }
    
    public static void setBroker(Broker broker) {
        set(KEY_BROKER, broker);
    }
    
    public static Integer getTenantId() {
        return (Integer) get(KEY_TENANT_ID);
    }
    
    public static void setTenantId(Integer tenantId) {
        set(KEY_TENANT_ID, tenantId);
    }
    
    public static void removeTenantId() {
        remove(KEY_TENANT_ID);
    }
    
    public static Integer removeOperatorId() {
        return (Integer) remove(KEY_OPERATOR_ID);
    }
    
    public static void removeCustomer() {
        remove(KEY_CUSTOMER);
    }
    
    public static void removeBroker() {
        remove(KEY_BROKER);
    }
    
    public static void removeBrokerId() {
        remove(KEY_BROKER_ID);
    }
    
    public static Object get(String key) {
        return BizDataContext.get(key);
    }
    
    public static Object set(String key, Object value) {
        return BizDataContext.set(key, value);
    }
    
    public static Object remove(String key) {
        return BizDataContext.remove(key);
    }
    
    public static void clear() {
        BizDataContext.clear();
    }
    
    public static void setBrokerId(Long brokerId) {
        set(KEY_BROKER_ID, brokerId);
        DataContext.setBrokerId(ValueUtil.getString(brokerId));
    }
    
    public static Long getBrokerId() {
        Object brokerId = get(KEY_BROKER_ID);
        return brokerId == null ? null : (Long) brokerId;
    }
    
    public static void setClientId(Long clientId) {
        set(KEY_CLIENT_ID, clientId);
    }
    
    public static Long getClientIdNoException() {
        Object clientId = get(KEY_CLIENT_ID);
        return clientId == null ? null : (Long) clientId;
    }
    
    public static Long getClientId() {
        Long clientId = getClientIdNoException();
        if (clientId == null) {
            throw new HSBCSErrInfoException(HSBCPSErrorCodes.NO_PERMISSION);
        }
        return clientId;
    }
    
}
